﻿#region Copyright(C) 2012 FI Ltd. All rights reserved.

// ==================================================================================================
// Copyright(C) 2006 FI Ltd. All rights reserved.
//
// SYSTEM NAME	:   FI.Net Application Framework
// COMPONENT ID :   FI.Web.Mvc
// COMPONENT DESC:  Extension for ASP.NET MVC
//
// CREATED DATE/BY:  18 Aug 2018 / Tian Mi
//
// REVISION HISTORY:	
// DATE/BY			SR#/CS/PM#/OTHERS		DESCRIPTION OF CHANGE
// ==================================================================================================

#endregion

using FI.Core.Properties;
using System;
using System.Reflection;

namespace FI.Core
{
    /// <summary>
    /// Localizable String
    /// </summary>
    public class LocalizableString
    {
        #region  Fields
        private Func<string> cachedResult;
        private string propertyName;
        private string propertyValue;
        private Type resourceType;
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the value.
        /// </summary>
        /// <value>
        /// The value.
        /// </value>
        public string Value
        {
            get
            {
                return this.propertyValue;
            }
            set
            {
                if (this.propertyValue != value)
                {
                    this.ClearCache();
                    this.propertyValue = value;
                }
            }
        }
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="LocalizableString"/> class.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        public LocalizableString(string propertyName)
        {
            this.propertyName = propertyName;
        }
        #endregion       

        #region Public Methods
        /// <summary>
        /// Gets the localizable value.
        /// </summary>
        /// <returns></returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
        public string GetLocalizableValue()
        {
            
            if (this.cachedResult == null)
            {
                if ((this.propertyValue == null) || (this.resourceType == null))
                {

                    Func<string> func2 = delegate
                        {
                            return this.propertyValue;
                        };
                    
                    this.cachedResult = func2;
                }
                else
                {
                    
                    PropertyInfo property = this.resourceType.GetProperty(this.propertyValue);
                    bool flag = false;
                    if ((!this.resourceType.IsVisible || (property == null)) || (property.PropertyType != typeof(string)))
                    {
                        flag = true;
                    }
                    else
                    {
                        MethodInfo getMethod = property.GetGetMethod();
                        if (((getMethod == null) || !getMethod.IsPublic) || !getMethod.IsStatic)
                        {
                            flag = true;
                        }
                    }
                    if (flag)
                    {
                        string exceptionMessage = ResourceUtility.Format(Resources.ExceptionLocalizationFailed, propertyName, resourceType.FullName, this.propertyValue );
                        this.cachedResult = delegate
                        {
                            throw new InvalidOperationException(exceptionMessage);
                        };
                    }
                    else
                    {
                       
                        Func<string> func = delegate
                        {
                            return (string)property.GetValue(null, null);
                        };
                        
                        this.cachedResult = func;
                    }
                }
            }
            return this.cachedResult();
        }

        /// <summary>
        /// Gets or sets the type of the resource.
        /// </summary>
        /// <value>
        /// The type of the resource.
        /// </value>
        public Type ResourceType
        {
            get
            {
                return this.resourceType;
            }
            set
            {
                if (this.resourceType != value)
                {
                    this.ClearCache();
                    this.resourceType = value;
                }
            }
        }
        #endregion

        #region Private Methods
        private void ClearCache()
        {
            this.cachedResult = null;
        }
        #endregion
    }
}
